package com.starry.JavaEEStudy;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;

public class RequestServlet5 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("getRequestURI=" + request.getRequestURI());// 资源路径 IP和端口号后面的内容 /javaeestudy/request5
        System.out.println("getRequestURL=" + request.getRequestURL());// 完整路径 http://localhost:8080/javaeestudy/request5
        System.out.println("getHeader的Content-Type=" + request.getHeader("Content-Type"));
        System.out.println("getContentType=" + request.getContentType());
        System.out.println("getRemoteHost=" + request.getRemoteHost()); // 0:0:0:0:0:0:0:1 (localhost访问) // 127.0.0.1(ip127.0.0.1访问)
        System.out.println("getRemoteAddr=" + request.getRemoteAddr()); // 0:0:0:0:0:0:0:1  // 127.0.0.1
        System.out.println("getMethod=" + request.getMethod()); // GET
        System.out.println("getQueryString=" + request.getQueryString());
        System.out.println("getServerName=" + request.getServerName());// localhost  // 127.0.0.1
        System.out.println("getRemotePort=" + request.getRemotePort());// 53516  // 53737  好像会变
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置请求体的字符集为utf-8编码 以防止中文乱码，注意一定要在获取请求参数前设置，即调用getParameter或getParameterValues之前，否则不生效
        // 比如先获取了密码 在设置字符集为utf-8,然后在获取中文的用户名，也不行，用户名会乱码
        // 请求体!!! 说明是针对的POST的
        request.setCharacterEncoding("UTF-8");
        request.getParameter("username");
        request.getParameterValues("username");
    }
}
